package com.hgts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgts.common.R;
import com.hgts.mapper.DetailsMapper;
import com.hgts.pojo.Book;
import com.hgts.pojo.Details;
import com.hgts.pojo.dto.BookDto;
import com.hgts.service.IBookService;
import com.hgts.service.IDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgts.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 曹怀金
 * @since 2023-05-03
 */
@Service
@Slf4j
public class DetailsServiceImpl extends ServiceImpl<DetailsMapper, Details> implements IDetailsService {


    @Autowired
    private IBookService bookService;

    @Override
    public R<Page> pageInfo(int page, int pageSize, String name) {
        Page<Details> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Details> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Details::getBookName,name).or().like(StringUtils.isNotEmpty(name),Details::getUsername,name);
        queryWrapper.eq(Details::getIsDelete,0);
        queryWrapper.eq(Details::getIsPromise,0);
        //执行查询
        this.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @Override
    public R<String> joinBooks(int... ids) {
        for (int id : ids) {
            LambdaUpdateWrapper<Details> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(Details::getIsPromise,1);
            updateWrapper.set(Details::getIsDelete,1);
            updateWrapper.eq(Details::getId,id);
            boolean update = this.update(updateWrapper);
            if (update){
                return R.success("操作成功");
            }
        }
        return R.error("操作失败");
    }

    @Override
    public R<Page> pageInfoR(int page, int pageSize, String name) {
        Page<Details> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Details> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Details::getBookName,name).or().like(StringUtils.isNotEmpty(name),Details::getUsername,name);
        queryWrapper.eq(Details::getIsPromise,1);
        //执行查询
        this.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }




}
